package com.xm.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xm.dao.UserDao;
import com.xm.dao.UserRoleDao;
import com.xm.pojo.User;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Autowired
    UserRoleDao userRoleDao;


    @Override
    public int saveUser(User user) {
        System.out.println("123");
        String password= user.getPassword();
        String slat= UUID.randomUUID().toString();
        SimpleHash simpleHash=new SimpleHash("MD5",password,slat,1  );

        user.setSalt(slat);
        user.setPassword(simpleHash.toHex());
        if(user.getValid()==null){
            user.setValid(1);
        }
        if(user.getRoleIds()==null){
            user.setRoleIds(new ArrayList<String>(){{this.add("1");}});
        }
        userDao.insertUser(user);
        return userRoleDao.insertUserRole(user);
    }

    @Override
    public int deleteById(Integer id) {
        userRoleDao.deleteByUserId(id);
        return userDao.deleteById(id);
    }

    @Override
    public int updateUser(User user) {
        userRoleDao.deleteByUserId(user.getId());
        userDao.updateById(user);
        return userRoleDao.insertUserRole(user);

    }

    @Override
    public List<User> findUser(User user) {
        return userDao.selectList(new QueryWrapper(user));

    }

    @Override
    public User findById(Integer id) {
        return userDao.selectById(id);
    }




}
